/**
*   \file   aabbtopoint.h
*   \author Dave Reid
*   \brief  Finds the distance between an AABB and a point.
*/
#ifndef __EAGL_DISTANCE_AABBTOPOINT_H_
#define __EAGL_DISTANCE_AABBTOPOINT_H_

#include "../types.h"

namespace eagl
{

/**
*   \brief  Finds the squared distance between a point and an AABB.
*/
inline float distance_aabbtopoint_sq(const vec3 &extents, const vec3 &position, const vec3 &point)
{
    float dist = 0.0f;

    for (int i = 0; i < 3; ++i)
    {
        float v = point[i];

        float aabb_min = position[i] - extents[i];
        float aabb_max = position[i] + extents[i];

        if (v < aabb_min)
        {
            dist += (aabb_min - v) * (aabb_min - v);
        }

        if (v > aabb_max)
        {
            dist += (v - aabb_max) * (v - aabb_max);
        }
    }

    return dist;
}

inline float distance_aabbtopoint(const vec3 &extents, const vec3 &position, const vec3 &point)
{
    return glm::sqrt(distance_aabbtopoint_sq(extents, position, point));
}

}

#endif  // __EAGL_DISTANCE_AABBTOPOINT_H_